<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="规则名称" prop="ruleName">
        <el-input
          v-model="queryParams.ruleName"
          placeholder="请输入规则名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['oa:humanClock:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['oa:humanClock:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading"
              :data="humanClockList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"  />
      <el-table-column label="规则名称"  prop="ruleName" />
      <el-table-column label="考勤规则">
        <template slot-scope="scope">
          <!-- `checked` 为 true 或 false -->
          <div v-for="item in scope.row.clockTimeList">
            <span>{{item.result}}</span><br/>
            <span>上班{{item.timeArray.startTime}} — 下班{{item.timeArray.endTime}}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="打卡地点" align="center">
        <template slot-scope="scope">
          <!-- `checked` 为 true 或 false -->
          <div v-for="item in scope.row.clockLocationList">
            <span>{{item.address}}</span><br/>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listHumanClock, getHumanClock, delHumanClock, addHumanClock, updateHumanClock } from "@/api/oa/humanClock";

export default {
  name: "ClockReportInfo",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 考勤规则表格数据
      humanClockList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ruleName: undefined,
        clockType: undefined,
        clockTime: undefined,
        enableSkipHoliday: undefined,
        clockLocation: undefined,
        enableReplaceCard: undefined,
        timeLimit: undefined,
        replaceNumberPermon: undefined,
        deadlinePermon: undefined,
        replaceMind: undefined,
        mindBefore: undefined,
        mindAfter: undefined,
        facialRecognitionCheck: undefined,
        recordSynchronization: undefined,
        whiteList: undefined,
        reporter: undefined,
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询考勤规则列表 */
    getList() {
      this.loading = true;
      listHumanClock(this.queryParams).then(response => {
        this.humanClockList = response.rows;
        this.humanClockList.forEach(d=>{
          d.clockTimeList=JSON.parse(d.clockTimeList);
          d.clockLocationList=JSON.parse(d.clockLocationList)
        })
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      // const userId = row.userId;
      this.$router.push({
        path:"/roster/clock-rule/editRule",
        query: {
          ruleId: ""
        }
      });
      // this.open = true;
      // this.title = "添加考勤规则";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push({
        path:"/roster/clock-rule/editRule",
        query: {
          ruleId: row.id
        }
      });

    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除考勤规则编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delHumanClock(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('oa/humanClock/export', {
        ...this.queryParams
      }, `humanClock_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style>
.el-card__header {
  border-bottom: none;
}
</style>
